<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 下午茶会员类
 * @package  module/base/customer
 * @author   create by allen.qiang
 * @version 1.0
 */

class ebsigCustTea {

    public function getCustomerInfo( &$arg_array ) {
        
        global $G_SHOP;

        //sql字符串
        $sql = '';

        //SQL字段类型字符串
        $params_type = '';

        //SQL字段值数组
        $params_array = array();

        //客户号
        if (isset($arg_array['pcustID']) && is_numeric($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }

        //会员名
        if (isset($arg_array['custID']) && is_numeric($arg_array['custID'])) {
            $sql .= ' AND custID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['custID'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        if (isset($arg_array['rp'])) {

            //统计数量
            $search_sql = 'SELECT COUNT(*) AS item_count FROM dunch_cust_master ' . $sql;
            $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            $item_count = $count_array[0]->item_count;
            if ($item_count == 0) {
                $arg_array['count'] = 0;
                return null;
            }

            $arg_array['count'] = $item_count;

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            //分页
            if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                $arg_array['page'] = 1;

            $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

        } else {

            if (empty($sql)) {
                return null;
            }

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname'])) {
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
            }

        }


        //查询分类信息
        $search_sql = 'SELECT * FROM dunch_cust_master ' . $sql;

        $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
        if (!$temp_item_array)
            return null;

        $item_array = array();

        foreach ($temp_item_array as &$item) {

            //用户信息数组
            $item_array[]  = array(
                'uuid'	    => 	$item->uuid,
                'timeStamp'	=>	$item->timeStamp,
                'creator' 	=> 	$item->creator,
                'createTime' => $item->createTime,
                'custID'	=> 	$item->custID,
                'pcustID'	=> 	$item->pcustID,
                'wx_openID'	=> 	$item->wx_openID,
                'brandID'	=>	$item->brandID,
                'is_right'	=> 	$item->is_right == 0 ? '╳' : '√',
                'is_treat'	=> 	$item->is_treat == 0 ? '╳' : '√',
                'is_coquetry'	=> 	$item->is_coquetry == 0 ? '╳' : '√',
                'createTime'	=> 	$item->createTime,
                'creator'	=> 	$item->creator
            );
        }

        return $item_array;

    }


    /**
     * 查询会员信息
     * @param array $arg_array 查询参数数组
     * @return null|array
     */
    public function searchCustomerInfo( &$arg_array ) {
        return $this->getCustomerInfo( $arg_array );
    }

}
